#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "FPGACtrl2_private.h"
#include "resource.h"
#include "proc.h"
#include "SerialPort.h"
#include "hub_ctrl.h"

#define TAB_NUM 4

HINSTANCE hInst;
HWND mDlgTab[TAB_NUM];

void LogEdit(HWND hDlg, int idc_edt, const char *str, ...)
{
	char str_buf[1024] = {0};
	va_list vlArgs;
	va_start(vlArgs, str);
	_vsnprintf_s(str_buf, sizeof(str_buf) - 1, sizeof(str_buf), str, vlArgs);
	va_end(vlArgs);
	SendDlgItemMessageA(hDlg, idc_edt, EM_SETSEL, -2, -1);
	SendDlgItemMessageA(hDlg, idc_edt, EM_REPLACESEL, TRUE, (LPARAM)str_buf);
	SendDlgItemMessageA(hDlg, idc_edt, WM_VSCROLL, SB_BOTTOM, 0);
}

void UpdateTab(HWND hParent)
{
	int nSelected = SendDlgItemMessage(hParent, IDC_TAB1, TCM_GETCURSEL, 0, 0);
	for (int i = 0; i < TAB_NUM; i++)
	{
		ShowWindow(mDlgTab[i], (i == nSelected) ? SW_SHOW : SW_HIDE);
	}
}

LRESULT Dlg1Main(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch (Msg)
	{
		case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDC_DLG1_BTN_OPEN:
					LogEdit(hDlg, IDC_DLG1_EDT1, "TAB%d\r\n",
					(hDlg == mDlgTab[0]) ? 1 : \
					(hDlg == mDlgTab[1]) ? 2 : \
					(hDlg == mDlgTab[2]) ? 3 : 4);
					break;
				case IDC_DLG2_BTN_OPEN:
					LogEdit(hDlg, IDC_DLG2_EDT1, "TAB%d\r\n",
					(hDlg == mDlgTab[0]) ? 1 : \
					(hDlg == mDlgTab[1]) ? 2 : \
					(hDlg == mDlgTab[2]) ? 3 : 4);
					break;
				case IDC_DLG3_BTN_OPEN:
					LogEdit(hDlg, IDC_DLG3_EDT1, "TAB%d\r\n",
					(hDlg == mDlgTab[0]) ? 1 : \
					(hDlg == mDlgTab[1]) ? 2 : \
					(hDlg == mDlgTab[2]) ? 3 : 4);
					break;
				case IDC_DLG4_BTN_OPEN:
					//printf("hDlg:%d mDlgTab:%d\r\n", hDlg, mDlgTab[3]);
					LogEdit(hDlg, IDC_DLG4_EDT1, "TAB%d\r\n",
					(hDlg == mDlgTab[0]) ? 1 : \
					(hDlg == mDlgTab[1]) ? 2 : \
					(hDlg == mDlgTab[2]) ? 3 : 4);
					break;
			}
			return TRUE;
	}
	return FALSE;
}

LRESULT MainDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	int ret;
	int cnt;
	char str[64] = {0};
	char str_por[] = "power on\r\n";
	CHAR pszText[TAB_NUM][16] = {TEXT("VU440"),TEXT("HAPS"),TEXT("PWR"),TEXT("HUB")};
	
	LPNMHDR etat = (LPNMHDR) lParam;
	switch (message)
	{
		case WM_INITDIALOG :
			RECT rcTabCtl;
			HWND hTabCtl;
			
			HICON hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON));
			SendMessage(hDlg, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
#ifdef DEBUG_EN
		InitConsoleWindow();
#endif
			Read_Tnifile();
			SerialPort();
			sprintf(str, "%s V%s", FILE_DESCRIPTION, VER_STRING);
			SetWindowText(hDlg, str);

			TCITEM tci = {0};
			HFONT hfont = CreateFontA(16, 0, //高，宽 0为系统选择最佳值
			                          0, 0, 0, 0, 0, 0, GB2312_CHARSET,
			                          OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
			                          DEFAULT_PALETTE | FF_DONTCARE, (TCHAR*)"Microsoft Sans Serif");
			
			tci.mask = TCIF_TEXT;
			hTabCtl = GetDlgItem(hDlg, IDC_TAB1);
			for (int i = 0; i < TAB_NUM; i++)
			{
				mDlgTab[i] = CreateDialog(hInst, MAKEINTRESOURCE(IDD_DLG1 + i), hTabCtl, Dlg1Main);
				tci.pszText = (LPTSTR)pszText[i];
				TabCtrl_InsertItem(hTabCtl, i, &tci);
			}
			GetClientRect(hTabCtl, &rcTabCtl);
			SendMessage(hTabCtl, TCM_ADJUSTRECT, FALSE, (LPARAM)&rcTabCtl);
			for (int i = 0; i < TAB_NUM; i++)
			{
				MoveWindow(mDlgTab[i], rcTabCtl.left - 3, rcTabCtl.top - 1, rcTabCtl.right - rcTabCtl.left + 4, rcTabCtl.bottom - rcTabCtl.top + 3, TRUE);
			}
			SendMessage(hTabCtl, TCM_SETCURSEL, 0, 0);

			cnt = GetPortList(g_data.port);
			printf("com cnt:%d\r\n", cnt);
			for (int idx = 0; idx < TAB_NUM; idx++)
			{
				SendDlgItemMessage(mDlgTab[i], IDC_DLG1_EDT1 + i, WM_SETFONT, (WPARAM)hfont, 0);
				for (int i = 0; i < cnt; i++)
				{
					printf("%s\r\n", (LPTSTR)g_data.port[i]);
					SendDlgItemMessage(mDlgTab[idx], IDC_DLG1_CBO + idx, CB_ADDSTRING, 0, (LPARAM)g_data.port[i]);
				}
				if (cnt)
				{
					SendDlgItemMessage(mDlgTab[idx], IDC_DLG1_CBO + idx, CB_SETCURSEL, cnt - 1, (LPARAM)0);
				}
			}
			UpdateTab(hDlg);
			return TRUE ;
		case WM_COMMAND :
			switch (LOWORD (wParam))
			{
				case IDOK :
				case IDCANCEL :
					DestroyWindow(hDlg);
					return TRUE ;
			}
			break ;
		case WM_NOTIFY:
			switch (etat->code)
			{
				case TCN_SELCHANGE:
					UpdateTab(hDlg);
					break;
			}
			break;
		case WM_CLOSE:
			DestroyWindow(hDlg);
			return TRUE;
		case WM_DESTROY:
			PostQuitMessage(0);
			return TRUE;
	};
	return FALSE;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	MSG msg;
	HWND hMainDlg = NULL;

	hInst = hInstance;
	hMainDlg = CreateDialog(hInstance, (LPCTSTR)IDD_MAIN_DIALOG, 0, (DLGPROC)MainDlgProc);
	ShowWindow(hMainDlg, nCmdShow);
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return 0;
}
